﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Tunnel
{
    class DBHelper
    {
        public static string connectionString = ConfigurationManager.ConnectionStrings["Tunnel.Properties.Settings.TunnelXAT1ConnectionString"].ToString();
        private static SqlConnection connection;
        public static SqlConnection Connection
        {
            get
            {
                string connectionString = "Data Source=LYS-PC;Initial Catalog=TunnelXAT1;Integrated Security=True";

                if (connection == null)
                {
                    connection = new SqlConnection(connectionString);

                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }

                return connection;
            }
        }

        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public static void CloseDb()
        {
            if ((connection != null) && (connection.State != ConnectionState.Closed))
            {
                connection.Close();
                connection = null;
            }
        }


        //返回影响行数。
        public static int ExecuteCommand(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            int iCount = cmd.ExecuteNonQuery();
            CloseDb();
            return iCount;
        }
        //返回影响行数
        public static int ExecuteCommand(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);

            int iCount = cmd.ExecuteNonQuery();
            CloseDb();
            return iCount;

        }

        //返回一个datatable
        public static DataSet GetTable(string sql)
        {

            SqlCommand cmd = new SqlCommand(sql, Connection);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            CloseDb();
            return ds;

        }
        public DataTable GetTable2(string sql)
        {

            SqlCommand cmd = new SqlCommand(sql, Connection);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataTable ds = new DataTable();
            da.Fill(ds);
            CloseDb();
            return ds;

        }

        //返回一个datatable


        public static DataTable GetTable(string StoredProcedureName, params SqlParameter[] pares)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(StoredProcedureName, Connection);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            if (pares != null)
            {
                foreach (SqlParameter pare in pares)
                {
                    da.SelectCommand.Parameters.Add(pare);
                }
            }
            da.Fill(ds);
            return ds.Tables[0];
        }

        public static int GetScalar(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            CloseDb();
            return result;
        }
        public static int GetScalar(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            CloseDb();
            return result;
        }
        public static SqlDataReader GetReader(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
        public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
    }
}
